package com.haruhinav.controller;


import com.auth0.jwt.JWT;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.haruhinav.Utils.JWTUtils;
import com.haruhinav.entity.User;
import com.haruhinav.entity.vo.ResultVO;
import com.haruhinav.service.UserService;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;
@RestController
@RequestMapping("/api")
public class LoginController {
    @Resource
    private UserService userService;


    @PostMapping("/e/login")
    public ResultVO login(@RequestBody User user){
//        String s = "1";
//        int a = Integer.parseInt(s); //string 转 int

        ResultVO result = new ResultVO();
        User findUser = userService.loginFindUser(user.getMid(),user.getPassword());
        if(findUser!=null){
            Map<String,Object> datatMap = new HashMap<>();
            Map<String,String> tokenMap = new HashMap<>();
            tokenMap.put("userid",findUser.getId().toString());
            tokenMap.put("usermid",findUser.getMid());

            datatMap.put("user",findUser);
            datatMap.put("token",JWTUtils.createToken(tokenMap));

            result.setCode(0);
            result.setMsg("登录成功");
            result.setData(datatMap);


        }else{
            result.setCode(-1);
            result.setMsg("账号或密码错误~");
        }
        return result;
    }
}
